package com.example.demo.config;

import com.example.demo.model.User;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MappedTypes(User.Gender.class)
public class GenderTypeHandler extends BaseTypeHandler<User.Gender> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, User.Gender parameter, JdbcType jdbcType)
            throws SQLException {
        ps.setString(i, parameter.name());
    }

    @Override
    public User.Gender getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String value = rs.getString(columnName);
        return value == null ? null : User.Gender.valueOf(value);
    }

    @Override
    public User.Gender getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String value = rs.getString(columnIndex);
        return value == null ? null : User.Gender.valueOf(value);
    }

    @Override
    public User.Gender getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String value = cs.getString(columnIndex);
        return value == null ? null : User.Gender.valueOf(value);
    }
} 